package cn.iocoder.yudao.module.member.controller.admin.userwithdraw;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.userwithdraw.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.userwithdraw.UserWithdrawDO;
import cn.iocoder.yudao.module.member.service.userwithdraw.UserWithdrawService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 提现")
@RestController
@RequestMapping("/member/user-withdraw")
@Validated
public class UserWithdrawController {

    @Resource
    private UserWithdrawService userWithdrawService;

    @PostMapping("/create")
    @Operation(summary = "创建提现")
    @PreAuthorize("@ss.hasPermission('member:user-withdraw:create')")
    public CommonResult<Long> createUserWithdraw(@Valid @RequestBody UserWithdrawSaveReqVO createReqVO) {
        return success(userWithdrawService.createUserWithdraw(createReqVO));
    }

//    /**
//     * 提现编辑
//     *
//     * @param id
//     * @param files
//     * @param status
//     * @param remark
//     * @return
//     */
//    @PostMapping("/update")
//    @Operation(summary = "提现编辑")
//    public CommonResult<Boolean> update(
//            @Parameter(description = "提现id") @RequestParam(name = "id", required = false) Long id,
//            @Parameter(description = "上传的图片列表（最多1个图片文件）") @RequestParam(name = "files", required = false) MultipartFile[] files,
//            @Parameter(description = "提现状态") @RequestParam(name = "status", required = false) Integer status,
//            @Parameter(description = "支付宝单号") @RequestParam(name = "aliNo", required = false) String aliNo,
//            @Parameter(description = "备注") @RequestParam(name = "remark", required = false) String remark) {
//        return success(userWithdrawService.updateUserWithdraw(id, files, status, aliNo, remark));
//    }

    @PostMapping("/update")
    @Operation(summary = "提现编辑")
    public CommonResult<Boolean> update(@Valid @RequestBody UserWithdrawUpdateReqVO updateReqVO) {
        return success(userWithdrawService.updateUserWithdraw(updateReqVO));
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新提现状态")
    @PreAuthorize("@ss.hasPermission('member:user-withdraw:update')")
    public CommonResult<Boolean> updateUserWithdrawStatus(@Valid @RequestBody UserWithdrawUpdateReqVO updateReqVO) {
//        updateReqVO.setUserId(null);
//        updateReqVO.setWithdraw(null);
//        updateReqVO.setStatus(WithdrawStatusEnum.SUCCESS.getCode());
//        updateReqVO.setWithdrawUserName(null);
//        updateReqVO.setAliAccount(null);
        userWithdrawService.updateUserWithdraw(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除提现")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-withdraw:delete')")
    public CommonResult<Boolean> deleteUserWithdraw(@RequestParam("id") Long id) {
        userWithdrawService.deleteUserWithdraw(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得提现")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-withdraw:query')")
    public CommonResult<UserWithdrawRespVO> getUserWithdraw(@RequestParam("id") Long id) {
        UserWithdrawDO userWithdraw = userWithdrawService.getUserWithdraw(id);
        return success(BeanUtils.toBean(userWithdraw, UserWithdrawRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得提现分页")
    @PreAuthorize("@ss.hasPermission('member:user-withdraw:query')")
    public CommonResult<PageResult<UserWithdrawRespVO>> getUserWithdrawPage(@Valid @RequestBody UserWithdrawPageReqVO pageReqVO) {
        return success(userWithdrawService.getUserWithdrawPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出提现 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-withdraw:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserWithdrawExcel(@Valid UserWithdrawPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserWithdrawRespVO> list = userWithdrawService.getUserWithdrawPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "提现.xls", "数据", UserWithdrawRespVO.class, list);
    }

    @GetMapping("/statistics")
    @Operation(summary = "获得提现统计")
    public CommonResult<UserWithdrawStatisticsRespVO> getUserWithdrawStatistics() {
        return success(userWithdrawService.getUserWithdrawStatistics());
    }

}